using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using SsbTransportTestSuite.Services;
using Microsoft.Samples.SsbTransportChannel;
using SsbTransportTestSuite.Clients;
using System.Data.SqlClient;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
namespace SsbTransportTestSuite
{

    class TestSuite
    {

        

        internal static int DoOneWayTest(Binding clientBinding, Binding serviceBinding)
        {
            try
            {
                

                ServiceHost host = new ServiceHost(typeof(OneWayService),new Uri("http://localhost:8020/ssbtests/oneway/"));
                host.AddServiceEndpoint(typeof(IOrderProcessing), serviceBinding, TestUtils.ServiceEndpointAddress);
                ServiceMetadataBehavior smb=new ServiceMetadataBehavior();
                smb.HttpGetEnabled=true;
                smb.HttpGetUrl=new Uri("http://localhost:8020/ssbtests/oneway/");
                host.Description.Behaviors.Add(smb);
                host.Faulted += TestUtils.ServiceHost_Faulted;
                host.Open();
                TestOneWayClient client = new TestOneWayClient();
                client.RunClient(clientBinding);

                Console.WriteLine("Waiting for service to process messages");
                OneWayService.EndOrderCalled.WaitOne();

                System.Threading.Thread.Sleep(200);
                host.Close();

                return 0;
            }
            catch (Exception ex)
            {
                return ErrorReporter.ReportError(ex);
            }
        }
        internal static int DoTwoWayTest(Binding clientBinding, Binding serviceBinding)
        {
            try
            {
                ServiceHost host = new ServiceHost(typeof(TestTwoWayService));
                host.AddServiceEndpoint(typeof(IOrderProcessing), serviceBinding, TestUtils.ServiceEndpointAddress);
                host.Open();
                TestTwoWayClient client = new TestTwoWayClient();
                client.RunClient(clientBinding);

                System.Threading.Thread.Sleep(200);
                host.Close();

                return 0;
            }
            catch (Exception ex)
            {
                return ErrorReporter.ReportError(ex);
            }
        }
        internal static int DoPersistentTest(Binding clientBinding, Binding serviceBinding)
        {
            try
            {

                ServiceHost host = new ServiceHost(typeof(TestPersistentService));
                ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IOrderProcessing), serviceBinding, TestUtils.ServiceEndpointAddress);
                endpoint.Behaviors.Add(new TestPersistentServiceInstanceProvider());
                host.Open();

                TestPersistentClient client = new TestPersistentClient();
                client.RunClient(clientBinding);

                System.Threading.Thread.Sleep(200);
                host.Close();

                return 0;
            }
            catch (Exception ex)
            {
                return ErrorReporter.ReportError(ex);
            }
        }
        internal static int DoEnlistingTest(Binding clientBinding, Binding serviceBinding)
        {
            try
            {
                ServiceHost host = new ServiceHost(typeof(TestTransactionEnlistingService));
                host.AddServiceEndpoint(typeof(IOrderProcessing), serviceBinding, TestUtils.ServiceEndpointAddress);
                
                host.Open();

                TestTransactionEnlistingClient client = new TestTransactionEnlistingClient();
                client.RunClient(clientBinding);

                System.Threading.Thread.Sleep(200);
                host.Close();
                return 0;
            }
            catch (Exception ex)
            {
                return ErrorReporter.ReportError(ex);
            }
        }
    }
}
